/*
	Copyright (c) 2011 Canadensys
*/
package net.canadensys.dataportal.vascan.config.impl;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

import net.canadensys.dataportal.vascan.config.InstallationConfigIF;


/**
 * Default implementation of the InstallationConfigIF as singleton.
 * This class keeps the config related to an installation of Vascan.
 * Those values can change from an installation to another.
 *
 */
public class InstallationConfig implements InstallationConfigIF{
	
	private static final InstallationConfig _instance = new InstallationConfig();
	private static final String CONFIG_NAME = "Vascan-Portal config";
	
	public static final String INSTALLATION_CONFIG_FILE_NAME = "installation_cfg.properties";
	
	public static final String LUCENE_DIR_KEY = "LuceneDir";
	public static final String GENERATED_IMAGES_DIR_KEY = "GeneratedImagesDir";
	public static final String GENERATED_CONTENT_DIR_KEY = "GeneratedContentDir";
	
	public static final String SITE_MAP_DIR_KEY = "SiteMapDir";
	public static final String GA_SITE_VERIFICATION_KEY = "GoogleAnalyticsSiteVerification";
	public static final String GA_ACCOUNT_KEY = "GoogleAnalyticsAccount";
	
	public static final String VASCAN_ROOT_URL_KEY = "VascanRootURL";
	public static final String PUBLIC_DOWNLOAD_URL_KEY = "PublicDownloadURL";
	
	private static final HashMap<String,String> config = new HashMap<String,String>();
	static{
		config.put(INSTALLATION_DIR_KEY, null);
		config.put(LUCENE_DIR_KEY, null);
		config.put(GENERATED_IMAGES_DIR_KEY, null);
		config.put(GENERATED_CONTENT_DIR_KEY, null);
		
		config.put(SITE_MAP_DIR_KEY, null);
		//Google Analytics related key
		config.put(GA_SITE_VERIFICATION_KEY, null);
		config.put(GA_ACCOUNT_KEY, null);
		
		config.put(VASCAN_ROOT_URL_KEY, null);
		config.put(PUBLIC_DOWNLOAD_URL_KEY, null);
	}
	
	private InstallationConfig(){}//hide the constructor, this is a singleton
	public static InstallationConfig getInstance(){return _instance;}
	
	@Override
	public void init(String webappFolder, Properties prop){
		prop.setProperty(InstallationConfigIF.INSTALLATION_DIR_KEY, webappFolder);
		
		String currKey;
		Enumeration<Object> keysEnum = prop.keys();
		if(keysEnum != null){
			while(keysEnum.hasMoreElements()){
				currKey = keysEnum.nextElement().toString();
				//if this element is handle by this class take it, otherwise, do nothing
				if(config.containsKey(currKey)){
					config.put(currKey, prop.getProperty(currKey));
				}
			}
		}
	}
	
	@Override
	public String get(String key){
		return config.get(key);
	}
	@Override
	public String getInstallationConfigFileName() {
		return INSTALLATION_CONFIG_FILE_NAME;
	}
	@Override
	public String toString(){
		return CONFIG_NAME;
	}
	
	/**
	 * Getter for a specific config as boolean
	 * If an error occur (unparsable, not found) false will be returned.
	 * @param key use the KEY constants of this class.
	 * @return
	 */
	public boolean getAsBool(String key){
		return Boolean.parseBoolean(config.get(key));
	}
}
